
    /**  
    * @Title: WebSocketChannelInitializer.java
    * @Package com.third.netty.coder.handler
    * @Description: 
    * @author likai
    * @date 2018年1月28日
    * @version V1.0  
    */
    
package com.plug.base.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
    * @ClassName: WebSocketChannelInitializer
    * @Description: webSocket协议的handler支持(参考：http://blog.csdn.net/wangjinnan16/article/details/78288205)
    * @author likai
    * @date 2018年1月28日
    *
    */

public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel>{
	@Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new HttpServerCodec());
        //以块的方式来写的处理器
        pipeline.addLast(new ChunkedWriteHandler());
        //netty是基于分段请求的，HttpObjectAggregator的作用是将请求分段再聚合,参数是聚合字节的最大长度
        pipeline.addLast(new HttpObjectAggregator(64 * 1024));
        pipeline.addLast(new HttpRequestHandler("/ws"));//判断请求路径是否正确
        //参数指的是contex_path
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));//WebSocketServerProtocol只处理WebSocket的Ping/Pong/Close请求，所以可以通过它帮忙将应用升级到WebSocket协议。

    }
}
